package vip.hello520.service;

import com.alicp.jetcache.anno.Cached;
import org.springframework.beans.factory.annotation.Autowired;
import vip.hello520.mapper.LoginMapper;
import vip.hello520.mapper.UserPoMapper;
import vip.hello520.model.ResultBean;
import vip.hello520.model.ResultEnum;
import vip.hello520.model.UserPo;

import java.util.Set;
/**
 * @author Hello
 * 如果加了@Service注解,就可以不用在dubbo配置文件里配置bean了,默认是类名首字母小写
 */
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private UserPoMapper userPoMapper;

    @Override
    //@CacheUpdate和@CacheInvalidate的name和area属性必须和@Cached相同，name属性还会用做cache的key前缀
    @Cached(name = "getUserPoByName-", key = "#name", expire = 60 * 60 * 24)
    public UserPo getUserPoByName(String name) throws Exception {
        if (null != name && !"".equals(name)) {
            return loginMapper.getUserByName(name);
        }
        return null;
    }

    @Override
    @Cached(name = "getRolePoListByUserId-", key = "#userId", expire = 60 * 60 * 24)
    public Set<String> getRolePoListByUserId(Integer userId) throws Exception {
        if (0 != userId) {
            return loginMapper.getRoleListByUserId(userId);
        }
        return null;
    }

    @Override
    @Cached(name = "getPowerPoListByUserId-", key = "#userId", expire = 60 * 60 * 24)
    public Set<String> getPowerPoListByUserId(Integer userId) throws Exception {
        if (null != userId) {
            return loginMapper.getPowerListByUserId(userId);
        }
        return null;
    }

    @Override
    public ResultBean regUser(UserPo userPo) throws Exception {
        ResultBean resultBean = new ResultBean();
        userPoMapper.insert(userPo);
        resultBean.setId(userPo.getId());
        resultBean.setState(ResultEnum.SUCCESS.getStateInt());
        resultBean.setMessage(ResultEnum.SUCCESS.getStateStr());
        return resultBean;
    }
}
